package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增菜品
     */
    @PostMapping
    public R<String> add(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("添加成功");
    }

    /**
     * 菜品分页查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(String name,Integer page,Integer pageSize){
        //准备分页条件
        Page<Dish> pageInfo1 = new Page<>(page,pageSize);
        //准备查询条件
        QueryWrapper<Dish> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.like(StringUtils.isNotBlank(name),"name",name);
        queryWrapper1.orderByAsc("sort").orderByDesc("update_time");
        dishService.page(pageInfo1,queryWrapper1);

        //1.创建page<DishDto>的分页属性
        Page<DishDto> pageInfo2 = new Page<>();
        //将dish里面的数据拷贝到dishDto中,但不包括records
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");

        //2.创建新的list集合
        ArrayList<DishDto> dishDtoList = new ArrayList<>();
        //遍历获取分类名称
        for (Dish dish : pageInfo1.getRecords()) {
            //将dish转换成dishDto
            DishDto dishDto = new DishDto();
            //将dish里面的值赋值给dishDto
            BeanUtils.copyProperties(dish,dishDto);
            //获取菜品分类id
            Long categoryId = dish.getCategoryId();
            //处理分类名称
            Category category = categoryService.getById(categoryId);
            //将分类名称赋值给dishDto
            dishDto.setCategoryName(category.getName());
            //再添加到集合中
            dishDtoList.add(dishDto);
        }
        pageInfo2.setRecords(dishDtoList);
        return R.success(pageInfo2);
    }

    /**
     * 根据id查询菜品
     */

    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品信息
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }

    /**
     * 查询菜品分类信息
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        //创建条件
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId()!=null,"category_id",dish.getCategoryId());
        queryWrapper.orderByAsc("sort").orderByDesc("update_time");
        queryWrapper.eq("status",1);
        queryWrapper.like(dish.getName()!=null,"name",dish.getName());
        List<Dish> dishList = dishService.list(queryWrapper);

        //1.将dish的对象转化成dishDto对象,并放在list集合中
        List<DishDto> dishDtoList = new ArrayList<>();
        //遍历dish集合
        for (Dish dish1 : dishList) {
            //将dish1拷贝到dishDto中
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish1,dishDto);
            //设置基本口味信息
            QueryWrapper<DishFlavor> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("dish_id",dishDto.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper1);
            //2.将口味信息添加到dishDtoList集合中
            dishDto.setFlavors(dishFlavorList);
            dishDtoList.add(dishDto);

        }

        return R.success(dishDtoList);
    }
}
